/**
 * Created by zoumap on 2019年12月16日
 * Copyright © 2019 zoumap.com
 * All rights reserved.
 */
package com.zgh.opcua.controller;

import com.zgh.opcua.manager.KepwareNodeManager;
import com.zgh.opcua.manager.KepwareSubscriptManager;
import com.zgh.opcua.manager.KepwareWriteManager;
import com.zgh.opcua.manager.OpcUaClientInstance;
import com.zgh.opcua.entity.ReadNodeEntity;
import com.zgh.opcua.entity.WriteNodeEntity;
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author zoumap
 * @ClassName: OpcUaController
 * @Description: OpcUa控制器
 * @date 2019年12月16日
 */
@Controller
public class CommonController {

    /**
     * @return
     * @MethodName: connect
     * @Description: opcua连接并订阅变量
     * @CreateTime 2019年12月16日 上午10:52:39
     */
    @RequestMapping("/connect")
    @ResponseBody
    public String connect() {

        try {
            OpcUaClient client = OpcUaClientInstance.getInstance().getOpcUaClient();
            if (client == null) {
                return "客户端配置实例化失败";
            }
            // 创建连接
            client.connect().get();
            return "创建连接成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    /**
     * @return
     * @MethodName: disconnect
     * @Description: disconnect
     * @CreateTime 2019年12月18日 上午10:48:46
     */
    @RequestMapping("/disconnect")
    @ResponseBody
    public String disconnect() {

        try {
            OpcUaClientInstance.getInstance().shutdown();
            return "断开连接成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    /**
     * @return
     * @MethodName: subscribe
     * @Description: subscribe
     * @CreateTime 2019年12月18日 上午10:49:06
     */
    @RequestMapping("/subscribe")
    @ResponseBody
    public String subscribe(HttpServletRequest request) {

        try {
            List<ReadNodeEntity> nodes = Arrays.asList(
                    ReadNodeEntity.builder().index(2).identifier("模拟器示例.函数.Ramp1").build(),
                    ReadNodeEntity.builder().index(2).identifier("模拟器示例.函数.Ramp2").build(),
                    ReadNodeEntity.builder().index(2).identifier("模拟器示例.函数.Ramp3").build()
            );
            KepwareSubscriptManager kepwareSubscriptManager = KepwareSubscriptManager.getInstance();
            return kepwareSubscriptManager.subscribeNodes(nodes);
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    /**
     * @return
     * @MethodName: subscribe
     * @Description: subscribe
     * @CreateTime 2019年12月18日 上午10:49:06
     */
    @RequestMapping("/caclesubscribe")
    @ResponseBody
    public String calesubscribe(HttpServletRequest request) {

        try {
            List<ReadNodeEntity> nodes = Arrays.asList(
                    ReadNodeEntity.builder().index(2).identifier("模拟器示例.函数.Ramp1").build(),
                    ReadNodeEntity.builder().index(2).identifier("模拟器示例.函数.Ramp2").build(),
                    ReadNodeEntity.builder().index(2).identifier("模拟器示例.函数.Ramp3").build()
            );
            KepwareSubscriptManager kepwareSubscriptManager = KepwareSubscriptManager.getInstance();
            kepwareSubscriptManager.unsubscribeNodes(nodes);
            return "取消成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    /**
     * @param request
     * @return
     * @MethodName: write
     * @Description: 节点写入
     * @CreateTime 2019年12月18日 上午9:59:23
     */
    @RequestMapping("/write")
    @ResponseBody
    public String write(HttpServletRequest request) {

        WriteNodeEntity node = WriteNodeEntity.builder().index(2).identifier(request.getParameter("id"))
                .value(request.getParameter("value")).type(request.getParameter("type")).build();

        try {
            KepwareWriteManager kepwareWriteManager = new KepwareWriteManager();
            CompletableFuture<String> stringCompletableFuture = kepwareWriteManager.writeAsync(node);
            CompletableFuture.allOf(stringCompletableFuture).join();
            return String.valueOf(stringCompletableFuture.isDone());
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }

    /**
     * @return 所有可访问的变量节点
     * @MethodName: browseAllVariables
     * @Description: 浏览所有可访问的变量节点
     * @CreateTime 2023年5月15日
     */
    @RequestMapping("/browse-all")
    @ResponseBody
    public ResponseEntity<?> browseAllVariables() throws Exception {
        try {
            KepwareNodeManager kepwareNodeManager = KepwareNodeManager.getInstance();
            return ResponseEntity.ok(kepwareNodeManager.browseAllNodes());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body("浏览所有变量节点失败: " + e.getMessage());
        }
    }
}
